package tmproject.hlhj.fhp.tmvote.activitys

import android.content.Intent
import android.graphics.Bitmap
import android.os.Environment
import kotlinx.android.synthetic.main.aty_camera.*
import org.greenrobot.eventbus.EventBus
import tmproject.hlhj.fhp.tmvote.BaseAty
import tmproject.hlhj.fhp.tmvote.R
import tmproject.hlhj.fhp.tmvote.beans.TempEvent
import tmproject.hlhj.fhp.tmvote.camera.JCameraView
import tmproject.hlhj.fhp.tmvote.camera.listener.ErrorListener
import tmproject.hlhj.fhp.tmvote.camera.listener.JCameraListener
import tmproject.hlhj.fhp.tmvote.log
import tmproject.hlhj.fhp.tmvote.presenters.CameraPresenter
import tmproject.hlhj.fhp.tmvote.views.CameraView
import java.io.File

/**
 * Created by Never Fear   on 2018\8\14 0014.
Never More....
 */
class CameraAty :BaseAty<CameraView,CameraPresenter>(), CameraView {
    private var bit:Bitmap?=null
    override fun bindView(): CameraView {
        return this
    }

    override fun createPresenter(): CameraPresenter {
        return CameraPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_camera
    }

    override fun initView() {
//设置视频保存路径
//设置视频质量
        camera.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        camera.setTip("")
        camera.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE)
    }

    override fun logicStart() {
    }

    override fun setListener() {
        camera.setErrorLisenter(object : ErrorListener {
            override fun onError() {
                log("fhp","录制失败")
            }
            override fun AudioPermissionError() {
                log("fhp","没有录音权限")
            }
        })

        camera.setJCameraLisenter(object :JCameraListener{
            override fun captureSuccess(bitmap: Bitmap?) {
                log("fhp","得到Bitmap")
                val tempEvent = TempEvent()
                tempEvent.bitmap=bitmap
                EventBus.getDefault().postSticky(tempEvent)
                finish()
            }
            override fun recordSuccess(url: String?, firstFrame: Bitmap?) {

            }
        })

        camera.setRightClickListener {
            log("fhp","点击了没。。。")
            val intent = Intent()
            intent.putExtra("bitmap",bit)
            setResult(-1,intent)
            finish()
        }

        camera.setLeftClickListener{
            finish()
        }
    }
}